package com.exemplo.flickrsocial.view;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.exemplo.flickrsocial.R;
import com.exemplo.flickrsocial.model.FlickrSocialModel;
import com.exemplo.flickrsocial.util.ImageUtils;

public class ImagePreview extends Activity implements OnClickListener {

	private ImageView part1;
	private ImageView part2;
	private ImageView part3;
	private ImageView part4;
	private FlickrSocialModel flickrModel;
	private Bitmap imagePick;
	private Button saveGallery;
	private Button cancel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(com.exemplo.flickrsocial.R.layout.image_preview);

		flickrModel = new FlickrSocialModel(getApplicationContext());

		Bundle bundle = this.getIntent().getExtras();

		imagePick = bundle.getParcelable("bitmap");
		initComponet();

		pintImage(getImageParts(imagePick));

	}

	private Bitmap[] getImageParts(Bitmap originalBitMap) {

		return ImageUtils.splitBitmap(originalBitMap);

	}

	private void initComponet() {

		this.saveGallery = (Button) findViewById(R.idImagePreview.saveGallery);
		this.cancel = (Button) findViewById(R.idImagePreview.cancel);
		this.saveGallery.setOnClickListener(this);
		this.cancel.setOnClickListener(this);
		
		this.part1 = (ImageView) findViewById(R.idImagePreview.part1);
		this.part2 = (ImageView) findViewById(R.idImagePreview.part2);
		this.part3 = (ImageView) findViewById(R.idImagePreview.part3);
		this.part4 = (ImageView) findViewById(R.idImagePreview.part4);
	}

	/**
	 * painted image if chosen
	 * 
	 * @param images
	 * @return
	 */
	private void pintImage(Bitmap[] images) {

		part1.setImageBitmap(images[0]);
		part2.setImageBitmap(ImageUtils.pintBitMap(images[1], Color.GREEN));
		part3.setImageBitmap(ImageUtils.pintBitMap(images[2], Color.GREEN));
		part4.setImageBitmap(images[3]);

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.idImagePreview.saveGallery:

			Images.Media.insertImage(getContentResolver(), imagePick,
					"flickrSocial", "");

			Toast.makeText(getApplication(), "Salvo na galeria.",
					Toast.LENGTH_SHORT);
			finish();

			break;
		case R.idImagePreview.cancel:

			finish();
			break;

		}
	}
}
